package com.xudow.app.dynamicstate_old.module.collect;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jakewharton.rxbinding.view.RxView;
import com.jude.beam.bijection.RequiresPresenter;
import com.jude.beam.expansion.list.BeamListFragment;
import com.jude.beam.expansion.list.ListConfig;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.jude.easyrecyclerview.decoration.DividerDecoration;
import com.jude.utils.JUtils;
import com.xudow.app.R;
import com.xudow.app.XApplication;
import com.xudow.app.dynamicstate_old.domain.Config;
import com.xudow.app.dynamicstate_old.domain.entity.Dynamic;
import com.xudow.app.dynamicstate_old.module.follow.DynamicDetailActivity;
import com.xudow.app.dynamicstate_old.module.follow.NewDynamicActivity;
import com.xudow.app.dynamicstate_old.module.follow.viewholder.DynamicContentViewHolder;
import com.xudow.app.dynamicstate_old.module.follow.viewholder.PostLongViewHolder;
import com.xudow.app.dynamicstate_old.module.follow.viewholder.PostNormalViewHolder;
import com.xudow.app.dynamicstate_old.module.follow.viewholder.RepostViewHolder;
import com.xudow.app.dynamicstate_old.module.user.UserDynamicPresenter;

@RequiresPresenter(CollectDynamicPresenter.class)
public class CollectDynamicFragment extends BeamListFragment<CollectDynamicPresenter, Dynamic> implements RecyclerArrayAdapter.OnItemClickListener {
    private static final int POST_NORMAL = 1;
    private static final int POST_LONG = 2;
    private static final int REPOST = 3;

    public static CollectDynamicFragment newInstance() {
        CollectDynamicFragment fragment = new CollectDynamicFragment();
        return fragment;
    }

    @Override
    public ListConfig getConfig() {
        return super.getConfig().setRefreshAble(false)
                .setContainerErrorView(getEmptyView())
                .setContainerEmptyView(getEmptyView());
    }

    private View getEmptyView() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.view_no_dynamic, null, false);
        RxView.clicks(view.findViewById(R.id.tag_write))
                .subscribe(v -> {
                    startActivity(new Intent(getContext(), NewDynamicActivity.class));
                });
        return view;
    }

    @Override
    public BaseViewHolder<Dynamic> getViewHolder(ViewGroup parent, int viewType) {
        DynamicContentViewHolder viewHolder = null;
        if (viewType == POST_LONG) {
            viewHolder = new PostLongViewHolder(getPresenter().getAdapter(), parent);
        } else if (viewType == POST_NORMAL) {
            viewHolder = new PostNormalViewHolder(getPresenter().getAdapter(), parent);
        } else if (viewType == REPOST) {
            viewHolder = new RepostViewHolder(getPresenter().getAdapter(), parent);
        }
        return viewHolder;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getPresenter().getAdapter().setOnItemClickListener(this);
        getListView().addItemDecoration(new DividerDecoration(getResources().getColor(R.color.gray100), JUtils.dip2px(12)));
    }

    @Override
    public int getViewType(int position) {
        Dynamic dynamic = getPresenter().getAdapter().getItem(position);
        // 1：长文，2：普通
        int type = dynamic.getType();
        // 上级动态主键, 为0说明，这条动态不是转发的
        long pId = dynamic.getpId();
        if (pId != 0)
            return REPOST;
        else if (type == 1)
            return POST_LONG;
        else if (type == 2)
            return POST_NORMAL;
        return 0;
    }

    @Override
    public void onItemClick(int position) {
        Intent intent = new Intent(getActivity(), DynamicDetailActivity.class);
        Dynamic dynamic = getPresenter().getAdapter().getItem(position);
        intent.putExtra("dynamic", dynamic);
        ((Activity) getContext()).startActivityForResult(intent, Config.Code_NeedRefresh);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            getPresenter().onRefresh();
        }
    }
}